package com.just.bz_back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.just.bz_back.dao.RepairAdminDescriptionMapper;
import com.just.bz_back.dao.RepairMapper;
import com.just.bz_back.dao.RepairUserDescriptionMapper;
import com.just.bz_back.model.*;
import com.just.bz_back.model.dto.RepairPacking;
import com.just.bz_back.service.RepairService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : baibaiwucahng
 * @date : 2019/8/31  16:11
 */
@Service
public class RepairServiceImpl implements RepairService {

    @Resource
    private RepairMapper repairMapper;
    @Resource
    private RepairUserDescriptionMapper repairUserDescriptionMapper;
    @Resource
    private RepairAdminDescriptionMapper repairAdminDescriptionMapper;
    @Override
    public List<RepairUserDescription> ListUser(RepairUserDescription repairUserDescription) {

        RepairUserDescriptionExample repairUserDescriptionExample=new RepairUserDescriptionExample();
        repairUserDescriptionExample.createCriteria().andTypeIdEqualTo(repairUserDescription.getTypeId());
        return repairUserDescriptionMapper.selectByExampleWithBLOBs(repairUserDescriptionExample);
    }

    @Override
    public List<RepairAdminDescription> ListAdmin(RepairAdminDescription repairAdminDescription) {
        RepairAdminDescriptionExample repairAdminDescriptionExample =new RepairAdminDescriptionExample();
        repairAdminDescriptionExample.createCriteria().andRepairUserDescriptionIdEqualTo(repairAdminDescription.getRepairUserDescriptionId());
        return repairAdminDescriptionMapper.selectByExampleWithBLOBs(repairAdminDescriptionExample);
    }

    @Override
    public int CreateRepair(RepairWithBLOBs repair) {
         repairMapper.insertSelective(repair);
         return 0;
    }

    @Override
    public PageInfo<RepairWithBLOBs> ListRepair(Repair repair) {
        PageHelper.startPage(repair.getPage().getPageNum(),repair.getPage().getPageSize());
        RepairExample repairExample=new RepairExample();
        RepairExample.Criteria criteria = repairExample.createCriteria().andStatusIn(new ArrayList<>(Arrays.asList(repair.getStatusall())));
        if (repair.getTypeId()!=null){
            criteria.andTypeIdEqualTo(repair.getTypeId());
        }
        if (!"".equals(repair.getUserId()))
        {
            criteria.andUserIdEqualTo(repair.getUserId());
        }
        if (!"".equals(repair.getUserMobile()))
        {
            criteria.andUserMobileEqualTo(repair.getUserMobile());
        }
        if (repair.getLocationId() != null)
        {
            criteria.andLocationIdEqualTo(repair.getLocationId());
        }
        if (!"".equals(repair.getUserRoom()))
        {
                criteria.andUserRoomEqualTo(repair.getUserRoom());
        }
        List<RepairWithBLOBs> repairWithBLOBs = repairMapper.selectByExampleWithBLOBs(repairExample);
        repairWithBLOBs= RepairPacking.PackRepair(repairWithBLOBs);
        PageInfo<RepairWithBLOBs> pageInfo=new PageInfo<>(repairWithBLOBs);
        return pageInfo;
    }
}
